package py.com.alfa.escriba.gui.controller.formatter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.format.Formatter;

public class DateFormatter implements Formatter<Date> {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public String print(Date object, Locale locale) {
		logger.info("Date Print: {}", object);
		Date value = object;
		String result = "";
		if (value != null) {
			result = new SimpleDateFormat("dd/MM/yyyy").format(value);
			logger.info("Result: {} - Value: {}", result, value);
		}
		return result;
	}

	@Override
	public Date parse(String text, Locale locale) throws ParseException {
		logger.info("Date text: {}", text);
		try {
			return new SimpleDateFormat("dd/MM/yyyy").parse(text);
		} catch (ParseException e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}

}
